****************************** 1. Preamble ******

* clean up 
	clear all 
	macro drop _all
	set max_memory 50g
	set matsize 11000

*set working directory. Must contain:
	cd "C:\Users\Public\Documents\BCG_DGM\"


****************************** 2. load the data ******	
****** WARANING NEED TO RUN BCG_tables_baseline
	
*Load the baseline firm-level file:
use BCG/data/full_firm_baseline_03.dta, replace


***************************** 3. Compute firm count *****
*B: might not needed if BCG_tables_baseline has just been run recently

*Compute number of firms in each 5digit sectors
	bysort naf_single year: egen firmcount_naf_va=count(va)
	bysort naf_single year: egen firmcount_naf_ca=count(catotal)
	bysort naf_single year: egen firmcount_naf_mu=count(mu_sepcal_TL_sFSQ_t1)
	su firmcount_naf_*
	
	*since all the same let us keep only one
	drop firmcount_naf_ca firmcount_naf_mu
	rename  firmcount_naf_va firmcount_naf


***************************** 3. Export the data in a single excel file *****


*Make the data a sector-level panel
duplicates drop naf_single year, force

*Keep the variables of interest
keep hhi firmcount_naf va_total catotal_total naf_single year

* Export data for calibration of the model
		
foreach var in hhi firmcount_naf va_total catotal_total{

	preserve 
		keep   `var' naf_single year
		reshape wide `var' , i(naf_single) j(year) 
		
		export excel "C:\Users\Public\Documents\BCG_DGM\BCG\output\exportdata_naf_rev2022.xls" , sheetmodify  firstrow(variables)   sheet("`var'")  
		
	restore
}
	
	
